import { defineStore } from 'pinia'
import { ref } from 'vue'
import { userInfoService } from '@/api/user'

// 用户模块
export const useUserStore = defineStore(
  'big-user',
  () => {
    // 用户token信息
    const token = ref('')
    const setToken = (newValue) => {
      token.value = newValue
    }
    const removeToken = () => {
      token.value = ''
    }
    // 用户基本信息
    const user = ref({})
    const getUserInfo = async () => {
      const res = await userInfoService()
      user.value = res.data.data
    }
    // 清除用户信息
    const setUser = (obj) => {
      user.value = obj
    }
    return {
      token,
      setToken,
      removeToken,
      user,
      getUserInfo,
      setUser
    }
  },
  {
    persist: true // 持久化
  }
)
